﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Oplaco.Exceptions;
using System.Web;
using Oplaco.Core.BusinessLogicLayer;
using System.Data;

namespace Oplaco.Web.DTO
{
    /// <summary>
    /// Collection object for ads
    /// </summary>
    public class CachedFeaturedAdvertismentCollection : ICollection<CachedFeaturedAdvertisment>
    {
        // The inner collection to store objects.
        private List<CachedFeaturedAdvertisment> innerCollection;
        // For IsReadOnly
        private bool isRO = false;
        
        
        #region Property
        public int Count
        {
            get { return innerCollection.Count; }
        }

        public bool IsReadOnly
        {
            get { return isRO; }
        }
        #endregion

        #region Public methods
        public void Add(CachedFeaturedAdvertisment item)
        {
            if (!Contains(item)) innerCollection.Add(item);
            else Oplaco.Exceptions.AppService.ProcessAndNotifyDataAccessLayerException(new Exception("Collection already contains the item : "+ item.Id.ToString()));
        }

        public void Clear()
        {
            innerCollection.Clear();
        }

        public bool Contains(CachedFeaturedAdvertisment item)
        {
            CachedFeaturedAdvertisment found = (from cf in innerCollection where cf.Equals(item) select cf).Single<CachedFeaturedAdvertisment>();
            return found==null ? true : false;
        }

        public void CopyTo(CachedFeaturedAdvertisment[] array, int arrayIndex)
        {
            innerCollection = (from item in array select item).ToList<CachedFeaturedAdvertisment>();
        }
        
        public bool Remove(CachedFeaturedAdvertisment item)
        {
            CachedFeaturedAdvertisment found = (from cf in innerCollection where cf.Equals(item) select cf).Single<CachedFeaturedAdvertisment>();
            if (found == null) return false;
            innerCollection.Remove(found);
            return true;
        }

        public IEnumerator<CachedFeaturedAdvertisment> GetEnumerator()
        {
            return new FeaturedAdvertismentCacheEnumerator(this);

        }
        #endregion

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return new FeaturedAdvertismentCacheEnumerator(this);

        }


        #region Construct
        public CachedFeaturedAdvertisment this[int index] {
            get { return (CachedFeaturedAdvertisment)innerCollection[index]; }
            set { innerCollection[index] = value; }
        }

        

        #endregion

        

        public class FeaturedAdvertismentCacheEnumerator : IEnumerator<CachedFeaturedAdvertisment>
        {
            private CachedFeaturedAdvertismentCollection innerCollection;
            private int curIndex;
            private CachedFeaturedAdvertisment curItem;

            public FeaturedAdvertismentCacheEnumerator(CachedFeaturedAdvertismentCollection collection)
            {
                innerCollection = collection;
                curIndex = -1;
                curItem = default(CachedFeaturedAdvertisment);
            }

            public CachedFeaturedAdvertisment Current
            {
                get { return innerCollection[curIndex]; }
            }

            public void Dispose()
            {
                
            }

            object System.Collections.IEnumerator.Current
            {
                get { return curItem; }
            }

            public bool MoveNext()
            {
                //Avoids going beyond the end of the collection.
                if (++curIndex >= innerCollection.Count)
                {
                    return false;
                }
                else
                {
                    // Set current box to next item in collection.
                    curItem = innerCollection[curIndex];
                }
                return true;

            }

            public void Reset()
            {
                curIndex = -1;
            }
        }
    }
}
